﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ExcelProcess;
using DataProcess;
using System.IO;

namespace importExcel
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 
  
    public partial class MainWindow : Window
    {
        private String inputFile;
        private String nodes;
        private int total;
        private int type;
        public MainWindow()
        {
            /*
             * 调用的时候需要传递一个数type进来
             * 0为舆情 1为谣言 2为意见领袖
             * 还需要一个存放sql文件的目录项
             * 这些都可以写成常量写在sysInfo里面，我现在不大好动，所以就先没改。
             */

            total = 1;
            nodes = "";
            type = 0;
           
            InitializeComponent();
            

        }

        private void ProgressBar_ValueChanged_1(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (nodes == "...") nodes = "";
            else nodes += '.';
            progressLabel.Content="已完成"+((ProgressBar)sender).Value.ToString()+"%"+nodes;
       //   if (progressBar.Value == 100) alertEnd(1);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog();
            op.ShowDialog();
            inputFile = op.FileName;
            labelOfFileName.Content = op.FileName.Split('\\').Last();
            
         //   progressBar.Value = 10;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            ExcelExtracter ee = new ExcelExtracter(inputFile,sheetName.Text , inputFile+".tmp");
            total=ee.Execute()-1;
            DataImporter di = new DataImporter(inputFile + ".tmp",type);
            KeyValuePair<int, int> result = di.Execute();
            progressBar.Value = 50;
            //DataProcess.DataProcess dp = new DataProcess.DataProcess(inputFile + ".tmp",this);
            if (File.Exists(inputFile + ".tmp"))
                File.Delete(inputFile + ".tmp");
            progressBar.Value = 100;
            MessageBox.Show("已完成导入，工作表中共" + (result.Key+result.Value) + "条数据，成功" + result.Key + "条，失败" + result.Value + "条。");
        }

        public void updateProgress(int now)
        {
            int num=(int)(Math.Truncate(now*100.0/total));
            progressBar.Value = num;

        }

        public void alertEnd(int fail)
        {
            MessageBoxResult mbr = MessageBox.Show("已完成导入，工作表中共"+total.ToString()+"条数据，失败"+fail.ToString()+"条");
        }

        public void alertTest(string x)
        {
            MessageBoxResult mbr = MessageBox.Show(x);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            MessageBoxButton mbb = MessageBoxButton.OKCancel;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBoxResult result=MessageBox.Show("确认要初始化该数据库？\n该操作将清空该库中所有现有数据", "提示", mbb, icon);

            if (result == MessageBoxResult.OK)
            {
                ClearAll.clear(type);
                MessageBox.Show("初始化完毕");
            }
        }

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            switch (((RadioButton)sender).Uid)
            {
                case "v0": type = 0; break;
                case "v1": type = 1; break;
                case "v2": type = 2; break;
                default: type = 0; break;

            }
        }
    }
}
